package cn.admobiletop.adsuyi.adapter.gdt.loader;

import android.app.Activity;

import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeUnifiedAD;

import cn.admobiletop.adsuyi.ad.ADSuyiNativeAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.NativeAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.NativeExpressAdListener;
import cn.admobiletop.adsuyi.config.ADSuyiConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 信息流广告加载器
 *              需要实现ADSuyiAdapterLoader<ADSuyiNativeAd, ADSuyiNativeAdListener>
 * @date 2020/4/8
 */
public class NativeAdLoader implements ADSuyiAdapterLoader<ADSuyiNativeAd, ADSuyiNativeAdListener> {
    private NativeExpressAdListener nativeExpressAdListener;
    private NativeAdListener nativeAdListener;

    /**
     * 加载信息流广告
     * @param nativeAd
     * @param adapterParams
     * @param adListener
     */
    @Override
    public void loadAd(ADSuyiNativeAd nativeAd, ADSuyiAdapterParams adapterParams, ADSuyiNativeAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(nativeAd)
                && adapterParams != null
                && adapterParams.getPlatform() != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId adSuyiPlatformPosId = adapterParams.getPlatformPosId();
            // 广告加载的Activity
            Activity activity = nativeAd.getActivity();
            // 广告平台广告位ID
            String platformPosId = adSuyiPlatformPosId.getPlatformPosId();

            if (ADSuyiConfig.RenderType.RENDER_TYPE_NATIVE == adSuyiPlatformPosId.getRenderType()) {
                loadNativeAd(activity, platformPosId, adapterParams.getCount(), adListener);
            } else {
                loadNativeExpressAd(activity, platformPosId, adapterParams.getCount(), adListener);
            }
        }
    }

    /**
     * 加载广点通模板信息流广告
     * @param activity      广告加载广告的Activity
     * @param platformPosId 广告平台广告位ID
     * @param count         广告平台请求广告数量
     * @param adListener    广告监听回调
     */
    private void loadNativeExpressAd(Activity activity, String platformPosId, int count, ADSuyiNativeAdListener adListener) {
        // 构造信息流监听类，实现广点通信息流广告回调接口
        nativeExpressAdListener = new NativeExpressAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通NativeExpressAD对象，传入必要的参数
        NativeExpressAD nativeExpressAD = new NativeExpressAD(activity
                , new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT)
                , platformPosId
                , nativeExpressAdListener);
        // 加载广点通信息流模版广告
        nativeExpressAD.loadAD(count);
    }

    /**
     * 加载广点通原生自渲染广告
     * @param activity      广告加载广告的Activity
     * @param platformPosId 广告平台广告位ID
     * @param count         广告平台请求广告数量
     * @param adListener    广告监听回调
     */
    private void loadNativeAd(Activity activity, String platformPosId, int count, ADSuyiNativeAdListener adListener) {
        // 构造信息流监听类，实现广点通信息流广告回调接口
        nativeAdListener = new NativeAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通NativeExpressAD对象，传入必要的参数
        NativeUnifiedAD nativeUnifiedAd = new NativeUnifiedAD(activity
                , platformPosId
                , nativeAdListener);
        // 加载广点通信息流原生自渲染广告
        nativeUnifiedAd.loadData(count);
    }

    @Override
    public void onResumed() {
        if (nativeAdListener != null) {
            nativeAdListener.resume();
        }
    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (nativeExpressAdListener != null) {
            nativeExpressAdListener.release();
            nativeExpressAdListener = null;
        }
        if (nativeAdListener != null) {
            nativeAdListener.release();
            nativeAdListener = null;
        }
    }

}
